using System;
using System.Collections.Generic;

namespace arcC
{
    static class Program
    {
        static string inputAssembly = "";
        static List<string> contentFolders;
        static string wrongFolder = "";
        static string extension = "";

        /// <summary>
        /// Entry point
        /// </summary>
        static void Main(string[] args)
        {
            ParameterErrors errors = parseParameters(args);
            if (errors == ParameterErrors.None)
            {
                ArchetypeCompiler ac = new ArchetypeCompiler();
                ac.Compile(inputAssembly, contentFolders, extension);
            }
            else
            {
                displayErrors(errors);
            }
        }

        public enum ParameterErrors
        {
            None,
            InvalidParametersCount,
            InputAssemblyFileNotFound,
            ContentFolderNotFound,
        }

        private static ParameterErrors parseParameters(string[] args)
        {
            //check parameter numbers
            if (args.Length < 3) return ParameterErrors.InvalidParametersCount;

            //
            extension = args[0];

            //check input assembly
            inputAssembly = args[1];
            if (!System.IO.File.Exists(inputAssembly))
            {
                return ParameterErrors.InputAssemblyFileNotFound;
            }

            //create a list with the content folders
            contentFolders = new List<string>();
            for (int i = 2; i < args.Length; i++)
            {
                contentFolders.Add(args[i]);
            }

            //check folders
            wrongFolder = "";
            foreach (string folder in contentFolders)
            {
                if (!System.IO.Directory.Exists(folder)) wrongFolder = folder;
            }

            //error if a folder was not found
            if (wrongFolder != "") return ParameterErrors.ContentFolderNotFound;

            return ParameterErrors.None;
        }

        private static void displayErrors(ParameterErrors errors)
        {
            switch (errors)
            {
                case ParameterErrors.InputAssemblyFileNotFound:
                    Console.WriteLine(String.Format("Input file not found: {0}", inputAssembly));
                    break;
                case ParameterErrors.ContentFolderNotFound:
                    Console.WriteLine(String.Format("Invaild content folder specified: {0}", wrongFolder));
                    break;
                case ParameterErrors.InvalidParametersCount:
                    Console.WriteLine("Invalid parameters. USAGE: arcC AssemblyFile [ContentFolder1] ... [ContentFolderN]");
                    break;
            }
        }
    }
}

